//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.planw.beetl.utils;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public final class JavaUtils {
    private JavaUtils() {
        throw new UnsupportedOperationException();
    }

    public static @NotNull PsiClass[] findClazzs(@NotNull Project project, @NotNull String name) {
        if (project == null) {
            return null;
        }

        if (name == null) {
            return null;
        }

        PsiClass[] var10000 = PsiShortNamesCache.getInstance(project).getClassesByName(name, GlobalSearchScope.allScope(project));
        if (var10000 == null) {
            return null;
        }

        return var10000;
    }

    public static @NotNull PsiField[] findSettablePsiFields(@NotNull PsiClass clazz) {
        if (clazz == null) {
            return null;
        }

        PsiField[] fields = clazz.getAllFields();
        List<PsiField> settableFields = new ArrayList(fields.length);

        for (PsiField f : fields) {
            PsiModifierList modifiers = f.getModifierList();
            if (modifiers == null || !modifiers.hasModifierProperty("static") && !modifiers.hasModifierProperty("final")) {
                settableFields.add(f);
            }
        }

        PsiField[] var10000 = (PsiField[]) settableFields.toArray(new PsiField[0]);
        if (var10000 == null) {
            return null;
        }

        return var10000;
    }

    public static boolean isElementWithinInterface(@Nullable PsiElement element) {
        if (element instanceof PsiClass && ((PsiClass) element).isInterface()) {
            return true;
        } else {
            PsiClass type = (PsiClass) PsiTreeUtil.getParentOfType(element, PsiClass.class);
            return Optional.ofNullable(type).isPresent() && type.isInterface();
        }
    }

    public static Optional<PsiClass> findClazz(@NotNull Project project, @NotNull String clazzName) {
        if (project == null) {
            return null;
        }

        if (clazzName == null) {
            return null;
        }

        return Optional.ofNullable(JavaPsiFacade.getInstance(project).findClass(clazzName, GlobalSearchScope.allScope(project)));
    }

    public static @NotNull Optional<PsiMethod> findMethod(@NotNull Project project, @Nullable String clazzName, @Nullable String methodName) {
        if (project == null) {
            return null;
        }

        if (!StringUtil.isEmptyOrSpaces(clazzName) && !StringUtil.isEmptyOrSpaces(methodName)) {
            Optional<PsiClass> clazz = findClazz(project, clazzName);
            if (clazz.isPresent()) {
                PsiMethod[] methods = ((PsiClass) clazz.get()).findMethodsByName(methodName, true);
                Optional var6 = ArrayUtil.isEmpty(methods) ? Optional.empty() : Optional.of(methods[0]);
                if (var6 == null) {
                    return null;
                }

                return var6;
            } else {
                Optional var5 = Optional.empty();
                if (var5 == null) {
                    return null;
                }

                return var5;
            }
        } else {
            Optional var10000 = Optional.empty();
            if (var10000 == null) {
                return null;
            }

            return var10000;
        }
    }

    public static List<PsiMethod> loadMapperPsiMethod(List<PsiClass> classMapperList, String method) {
        List<PsiMethod> nameMethodsList = new ArrayList();

        for (PsiClass psiClass : classMapperList) {
            PsiMethod[] ms = psiClass.getMethods();

            for (PsiMethod m : ms) {
                if (m.getName().equals(method)) {
                    nameMethodsList.add(m);
                }
            }
        }

        return nameMethodsList;
    }
}
